
package com.fau.wmbj.server.entity;

import cn.hutool.json.JSONUtil;
import com.fau.wmbj.server.utils.BaseAttributeConverter;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Map;

/**
 * Entity - 审计日志
 *
 * @author jiawei
 */

@Getter
@Setter
@Entity
@Table(name = "f_audit_log")
public class AuditLog extends BaseEntity<Long> implements Serializable {

    private static final long serialVersionUID = -4494144902110236826L;

    /**
     * "审计日志"属性名称
     */
    public static final String AUDIT_LOG_ATTRIBUTE_NAME = AuditLog.class.getName() + ".AUDIT_LOG";

    /**
     * 动作
     */
    @Column(nullable = false, updatable = false, name = "action")
    private String action;

    /**
     * IP
     */
    @Column(nullable = false, updatable = false, name = "ip")
    private String ip;

    /**
     * 请求URL
     */
    @Column(nullable = false, updatable = false, name = "request_url")
    private String requestUrl;

    /**
     * 请求参数
     */
    @Column(updatable = false, length = 4000, name = "parameters")
    @Convert(converter = ParameterConverter.class)
    private Map<String, String[]> parameters;

    /**
     * 用户信息
     */
    @Column(name = "user_info")
    private String userInfo;

    @Transient
    private String parameter;

    /**
     * 类型转换 - 请求参数
     *
     * @author jiawei
     */
    @Converter
    public static class ParameterConverter extends BaseAttributeConverter<Map<String, String[]>> {
    }

    @Transient
    public String getParameter() {
        if (getParameters() != null) {
            Map<String, String[]> parameters = getParameters();
            return JSONUtil.toJsonStr(parameters);
        }
        return "";
    }
}